<?php

/**
 * HTTP comunicate with client, send response, handling headers, set cookies, get client information.
 * @package Core
 * @category Core
 * @author kovacsricsi
 */
class HTTP
{
	/**
	 * Echo output.
	 * @access public
	 * @static
	 * @param AView $view
	 * @param string $header
	 * @return void
	 */
	static public function sendOutput(AView $view, $header)
	{
		if ($view->getTemplate() == "404.html") {
			$header = "404_headers.txt";
		}

		if ($fp = fopen(DIR_HEADERS . "/" . $header, "rb")) {
			while (!feof($fp))
			{
				self::sendHeader(fgets($fp), true);
			}
			fclose($fp);
		}

		self::write($view->getOutput(), false);
	}

	/**
	 * Send header.
	 * @access public
	 * @static
	 * @param string $header
	 * @param boolean $replace
	 * @param integer $code
	 * @return void
	 */
	public static function sendHeader($header, $replace = null, $code = 200)
	{
		header($header, $replace, $code);
	}

    /**
     * Returns with all setted headers
     * @access public
     * @static
     * @return array
     */
    public static function getHeaders()
    {
        return headers_list();
    }

	/**
	 * Write string to output.
	 * @access public
	 * @static
	 * @param string $sring
     * @param boolean $flush
	 * @return void
	 */
	public static function write($string, $flush = true)
	{
		echo (string)$string;

        if ($flush == true) {
            flush();
            ob_flush();
        }
	}

    /**
     * Set cookie and send to client.
     * @access public
     * @static
     * @param string $name
     * @param mixed $valie
     * @param int $expire
     * @param string $path
     * @param string $domain
     * @param boolean $secure
     * @param boolean $httpOnly
     * @return boolean
     */
    public static function setCookie($name, $value, $expire = null, $path = null, $domain = null, $secure = null, $httpOnly = null)
    {
        return setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly);
    }

    /**
     * Set cookie without urlencoding and send to client.
     * @access public
     * @static
     * @param string $name
     * @param mixed $valie
     * @param int $expire
     * @param string $path
     * @param string $domain
     * @param boolean $secure
     * @param boolean $httpOnly
     * @return boolean
     */
    public static function setRawCookie($name, $value, $expire = null, $path = null, $domain = null, $secure = null, $httpOnly = null)
    {
        return setrawcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly);
    }

    /**
     * Returns client ip address
     * @access public
     * @static
     * @return string
     */
    public static function getIpAddress()
    {
            $ip = false;

            if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
                $ip = $_SERVER["HTTP_CLIENT_IP"];
            }

            /**
             * User is behind a proxy and check that we discard RFC1918 IP addresses
             * if they are behind a proxy then only figure out which IP belongs to the
             * user.  Might not need any more hackin if there is a squid reverse proxy
             * infront of apache.
             */
            if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
                $ips = explode (", ", $_SERVER["HTTP_X_FORWARDED_FOR"]);

                if ($ip) {
                    array_unshift($ips, $ip);
                    $ip = false;
                }

                for ($i = 0; $i < count($ips); $i++) {
                    // Skip RFC 1918 IP's 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16
                    if (!preg_match("/^(?:10|172\.(?:1[6-9]|2\d|3[01])|192\.168)\./", $ips[$i])) {
                        if (version_compare(phpversion(), "5.0.0", ">=")) {
                            if (ip2long($ips[$i]) != false) {
                                $ip = $ips[$i];
                                break;
                            }
                        } else {
                            if (ip2long($ips[$i]) != -1) {
                                $ip = $ips[$i];
                                break;
                            }
                        }
                    }
                }
        }

            return ($ip != false) ? $ip : $_SERVER["REMOTE_ADDR"];
    }

    /**
     * Return client agent info
     * @access public
     * @static
     * @return string
     */
    public static function getUserAgent()
    {
        return $_SERVER["HTTP_USER_AGENT"];
    }

}

?>